using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;

public class CameraZoom : MonoBehaviour
{
    private float defaultCameraDistance = 6f;
    private float minCameraDistance = 1f;
    private float maxCameraDistance = 6f;

    private CinemachineFramingTransposer framingTransposer;
    private CinemachineInputProvider cinemachineInputProvider;

    private float smoothing = 10000f;

    private float zoomSensitivity = 1f;

    private float currentTargetDistance = 6f;

    private void Awake()
    {
        framingTransposer = GetComponent<CinemachineVirtualCamera>()
            .GetCinemachineComponent<CinemachineFramingTransposer>();
        cinemachineInputProvider = GetComponent<CinemachineInputProvider>();

        currentTargetDistance = defaultCameraDistance;
    }

    private void Update()
    {
        Zoom();
    }

    private void Zoom()
    {
        float zoomValue = cinemachineInputProvider.GetAxisValue(2) * zoomSensitivity;
        currentTargetDistance = Mathf.Clamp(currentTargetDistance + zoomValue, minCameraDistance, maxCameraDistance);

        if (Math.Abs(currentTargetDistance - framingTransposer.m_CameraDistance) < Mathf.Epsilon)
            return;

        float lerpedZoomValue = Mathf.Lerp(framingTransposer.m_CameraDistance, currentTargetDistance,
        smoothing * Time.deltaTime);

        framingTransposer.m_CameraDistance = lerpedZoomValue;
    }
}